package com.example.scoredemo

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.AsyncTask
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.SavedStateViewModelFactory
import androidx.lifecycle.ViewModelProvider
import com.example.scoredemo.dao.DaoUtil
import com.example.scoredemo.dao.entity.Student
import com.example.scoredemo.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    lateinit var mViewModel: MyViewModel
    lateinit var binding: ActivityMainBinding
    lateinit var sharePreferences: SharedPreferences
    private lateinit var _sharePreferences: SharedPreferences
    lateinit var daoutil: DaoUtil


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        binding.root
        mViewModel = ViewModelProvider(
                this,
                SavedStateViewModelFactory(application, this)
        )[MyViewModel::class.java]
        binding.lifecycleOwner = this
        binding.data = mViewModel

        sharePreferences = getPreferences(Context.MODE_PRIVATE)  //已Activity命名的
        _sharePreferences = getSharedPreferences("muzi", Context.MODE_PRIVATE)  //已Activity命名的
        sharePreferences.edit().putInt("age", 102).apply()

        val layoutInflater = LayoutInflater.from(this)

        //Navigation的使用
//        startActivity(Intent(this, HomeActivity::class.java))

        // Room数据库的使用
//        daoutil = DaoUtil.getInstance(MyApplication.instance)
//        val student0 = Student("三里一种", "梁忠贤", 3, "深圳")
//        val student1 = Student("三里一种", "小莫", 4, "深圳")
//        val student2 = Student("三里一种", "老王", 5, "深圳")
//        val student3 = Student("三里一种", "沈阳", 7, "深圳")
//        val insertStudent = InsertStuddent(daoutil)
//        insertStudent.execute(student0, student1, student2, student3)

        //自定义FlowLayout页面
//        startActivity(Intent(this, FlowLayoutActivity::class.java))
        //textVeiw 多文本对其问题
        startActivity(Intent(this, TestActivity::class.java))

    }


    /**
     * 内部静态类
     * AsyncTask
     */
    companion object {
        class InsertStuddent(daoUtil: DaoUtil) : AsyncTask<Student, String, String>() {

            var mDaoUtil: DaoUtil = daoUtil
            override fun onPreExecute() {
                //主线程
                super.onPreExecute()
                //准确前
            }

            override fun doInBackground(vararg params: Student?): String {
                //子线程
                params?.forEach {
                    mDaoUtil.insertStduent(it)
                }
                //传递进度值
                var students = mDaoUtil.allStudents
                students.forEach { Log.e("InsertStdudent", it!!.toString()) }
                return ""
            }

            override fun onProgressUpdate(vararg values: String?) {
                //UI线程
                super.onProgressUpdate(*values)

            }

            override fun onPostExecute(result: String?) {
                super.onPostExecute(result)
                //主线程
            }
        }
    }
}
